cc.Class({
    extends: cc.Component,

    properties: {
        near_bg: [cc.Node],
        far_bg: [cc.Node],

        near_speed: 15,
        far_speed: 0.5,
    },

    onLoad: function () {
        this.fixBgPos(this.far_bg[0], this.far_bg[1]);
        this.fixBgPos(this.near_bg[0], this.near_bg[1]);

        cc.log("背景OnLoad被触发~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", Date.now());

        this.schedule(function () {
            this.near_speed = Math.random() * 2 + 10;
            this.far_speed = Math.random() * 2 + 5;
            cc.log("时间:", Date.now(), "改变速度:", this.far_speed);
        }, 2);
    },

    fixBgPos: function (bg1, bg2) {
        bg1.x = 0;
        const bg1BoundingBox = bg1.getBoundingBox();
        bg2.x = bg1BoundingBox.xMax;
    },

    update: function (dt) {
        this.MoveSomeNode(this.near_bg, this.near_speed);
        this.MoveSomeNode(this.far_bg, this.far_speed);
    },

    MoveSomeNode: function (nodeArray, speed) {
        const node1 = nodeArray[0],
            node2 = nodeArray[1];

        node1.x -= speed;
        node2.x -= speed;

        if (node1.x <= 0)
            node2.x = node1.getBoundingBox().xMax;

        if (node2.x <= 0)
            node1.x = node2.getBoundingBox().xMax;

    },
});
